
import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class EchoClient {

   public static void main(String[] arstring) {
      btnSocket();
   }

   public List<String> makeCommand(String host, int port, String command) {
      //Hàm tạo kết nối và trả về danh sách các dòng nhận được
      List<String> rt = new ArrayList();
      try {
         Socket client = new Socket(host, port);
//         System.out.println("Just connected to " + client.getRemoteSocketAddress());
//         boolean meetExit = false;
         InputStream inputstream = System.in;
         InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
         BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

         OutputStream outputstream = client.getOutputStream();
         OutputStreamWriter outputstreamwriter = new OutputStreamWriter(outputstream);
         BufferedWriter bufferedwriter = new BufferedWriter(outputstreamwriter);
//         while (!meetExit) {
         InputStream inputstreamSv = client.getInputStream();
         InputStreamReader inputstreamreaderSv = new InputStreamReader(inputstreamSv);
         BufferedReader bufferedreaderSv = new BufferedReader(inputstreamreaderSv);
         String string;
         String svStr;
//         System.out.print("\nCommand: ");
         if ((string = bufferedreader.readLine()).equals("exit")) {
//            meetExit = true;
         } else {
            bufferedwriter.write(string + '\n');
            bufferedwriter.flush();
//               System.out.println(">>: " + string);
            String revStr = "";
            while (!"".equals(svStr = bufferedreaderSv.readLine().trim()) && svStr != null && !svStr.equals("End")) {
//                  System.out.println(svStr);
//               revStr += svStr;
               rt.add(svStr);
            }

//               System.out.println("<<: " + revStr);
//         }
         }
         bufferedwriter.close();
         client.close();
      } catch (IOException e) {
         e.printStackTrace();
      } finally {
         return rt;
      }
   }

   public static void btnSocket() {
      String serverName = "localhost";
      int port = 8181;
      try {
         System.out.println("Connecting to " + serverName + " on port " + port);
         Socket client = new Socket(serverName, port);
         System.out.println("Just connected to " + client.getRemoteSocketAddress());
         boolean meetExit = false;
         InputStream inputstream = System.in;
         InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
         BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

         OutputStream outputstream = client.getOutputStream();
         OutputStreamWriter outputstreamwriter = new OutputStreamWriter(outputstream);
         BufferedWriter bufferedwriter = new BufferedWriter(outputstreamwriter);
         while (!meetExit) {
            InputStream inputstreamSv = client.getInputStream();
            InputStreamReader inputstreamreaderSv = new InputStreamReader(inputstreamSv);
            BufferedReader bufferedreaderSv = new BufferedReader(inputstreamreaderSv);
            String string;
            String svStr;
            System.out.print("\nCommand: ");
            if ((string = bufferedreader.readLine()).equals("exit")) {
               meetExit = true;
            } else {
               bufferedwriter.write(string + '\n');
               bufferedwriter.flush();
               System.out.println(">>: " + string);
               String revStr = "";
               while (!"".equals(svStr = bufferedreaderSv.readLine().trim()) && svStr != null && !svStr.equals("End")) {
//                  System.out.println(svStr);
                  revStr += svStr;
               }
               System.out.println("<<: " + revStr);
            }
         }
         bufferedwriter.close();
         client.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}